package com.shop.service.impl;

import com.shop.common.ResponseResult;
import com.shop.entity.Role;
import com.shop.entity.vo.RightsVo;
import com.shop.entity.vo.RoleVo;
import com.shop.mapper.RightsMapper;
import com.shop.mapper.RoleMapper;
import com.shop.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RightsMapper rightsMapper;
    @Override
    public ResponseResult addRole(Role role) {
        ResponseResult result = new ResponseResult();
        if (role.getRoleName()==null){
            result.setINTERNALSERVERERROR();
            return result;
        }
        int insert = 0;
        try {
            insert = roleMapper.insert(role);
        }catch (Exception e){
            result.setINTERNALSERVERERROR();
            return result;
        }
        if (insert!=1){
            result.setINTERNALSERVERERROR();
            return result;
        }
        result.setCREATED("添加成功",role);
        return result;
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        ResponseResult result = new ResponseResult();
        Role role = roleMapper.selectById(id);
        if (role==null){
            result.setSuccess("没有找到相关内容");
        }
        result.setSuccess("获取成功",role);
        return result;
    }

    @Override
    public ResponseResult updateRole(Integer id, Role role) {
        ResponseResult result = new ResponseResult();
        role.setRoleId(id);
        int insert = 0;
        try {
            insert= roleMapper.updateById(role);
        }catch (Exception e){
            result.setINTERNALSERVERERROR();
            return result;
        }
        if (insert!=1){
            result.setINTERNALSERVERERROR();
            return result;
        }
        result.setSuccess("修改成功",role);
        return result;
    }

    @Override
    public ResponseResult deleteRole(Integer id) {
        ResponseResult result = new ResponseResult();
        int insert = roleMapper.deleteById(id);
        if (insert!=1){
            result.setINTERNALSERVERERROR();
            return result;
        }
        result.setDELETED();
        return result;
    }

    @Override
    public ResponseResult selectAll() {
        ResponseResult result = new ResponseResult();
        List<Role> roleList = roleMapper.selectList(null);//查询所有用户
        List<RightsVo> list = rightsMapper.selectPid();//查询所有的权限
        List<RoleVo> roleVoList = new ArrayList<>();
        for (Role role:roleList){
            RoleVo roleVo = new RoleVo();
            roleVo.setRoleId(role.getRoleId());
            roleVo.setRoleDesc(role.getRoleDesc());
            roleVo.setRoleName(role.getRoleName());
            if (role.getPsIds()==null||role.getPsIds().equals("")){
                roleVoList.add(roleVo);
                continue;
            }
            String[] arr1 = role.getPsIds().split(",");
            Integer[] arr= new Integer[arr1.length];
            for (int i = 0; i < arr1.length; i++) {
                arr[i]=Integer.parseInt(arr1[i]);
            }
            List<RightsVo> resultList = new ArrayList<>();
            for (RightsVo rightsVo:list){
                if (rightsVo.getLevel().equals("0")&&cha(arr,rightsVo.getId())){
                    List<RightsVo> secondList = new ArrayList<>();
                    for (RightsVo rightsVo1:list){
//                        System.out.println(rightsVo1.getId()+"|  |"+rightsVo1.getPid()+"|  |"+rightsVo.getId()+(rightsVo1.getPid()==rightsVo.getId()));
                        if (rightsVo1.getPid()==rightsVo.getId()&&cha(arr,rightsVo1.getId())){
                            List<RightsVo> thirdList = new ArrayList<>();
                            for (RightsVo rightsVo2:list){
                                if (rightsVo2.getPid()==rightsVo1.getId()&&cha(arr,rightsVo2.getId())){
                                    thirdList.add(rightsVo2);
                                }
                            }
                            rightsVo1.setChildren(thirdList);
                            secondList.add(rightsVo1);
                        }
                    }
                    rightsVo.setChildren(secondList);
                    resultList.add(rightsVo);
                }
            }
            roleVo.setChildren(resultList);
            roleVoList.add(roleVo);
        }
        result.setSuccess("获取成功",roleVoList);
        return result;
    }

    @Override
    public ResponseResult updateAuthorization(Integer id,String rids) {
        ResponseResult result = new ResponseResult();
        Role role = roleMapper.selectById(id);
        role.setPsIds(rids);
        int insert = 0;
        try {
            insert = roleMapper.updateById(role);
        }catch (Exception e){
            result.setINTERNALSERVERERROR();
        }
        if (insert==1){
            List<RightsVo> list = rightsMapper.selectPid();//查询所有的权限
            RoleVo roleVo = new RoleVo();
            roleVo.setRoleId(role.getRoleId());
            roleVo.setRoleDesc(role.getRoleDesc());
            roleVo.setRoleName(role.getRoleName());
            if (role.getPsIds()==null||role.getPsIds().equals("")){
                result.setSuccess("修改成功",roleVo);
                return result;
            }
            String[] arr1 = role.getPsIds().split(",");
            Integer[] arr= new Integer[arr1.length];
            for (int i = 0; i < arr1.length; i++) {
                arr[i]=Integer.parseInt(arr1[i]);
            }
            List<RightsVo> resultList = new ArrayList<>();
            for (RightsVo rightsVo:list){
                if (rightsVo.getLevel().equals("0")&&cha(arr,rightsVo.getId())){
                    List<RightsVo> secondList = new ArrayList<>();
                    for (RightsVo rightsVo1:list){
//                        System.out.println(rightsVo1.getId()+"|  |"+rightsVo1.getPid()+"|  |"+rightsVo.getId()+(rightsVo1.getPid()==rightsVo.getId()));
                        if (rightsVo1.getPid()==rightsVo.getId()&&cha(arr,rightsVo1.getId())){
                            List<RightsVo> thirdList = new ArrayList<>();
                            for (RightsVo rightsVo2:list){
                                if (rightsVo2.getPid()==rightsVo1.getId()&&cha(arr,rightsVo2.getId())){
                                    thirdList.add(rightsVo2);
                                }
                            }
                            rightsVo1.setChildren(thirdList);
                            secondList.add(rightsVo1);
                        }
                    }
                    rightsVo.setChildren(secondList);
                    resultList.add(rightsVo);
                }
            }
            roleVo.setChildren(resultList);
            result.setSuccess("修改成功",roleVo);
        }else {
            result.setINTERNALSERVERERROR();
        }
        return result;
    }

    @Override
    public ResponseResult deleteAuthorization(int roleId, int rightId) {
        ResponseResult result = new ResponseResult();
        Role role = roleMapper.selectById(roleId);
        String[] array1= role.getPsIds().split(",");
        int[] array2= new int[array1.length];
        for (int i = 0; i < array1.length; i++) {
            if(Integer.parseInt(array1[i])!=rightId){
                array2[i]=Integer.parseInt(array1[i]);
            }
        }
        String str="";
        for (int i = 0; i < array2.length; i++) {
            if (array2[i]!=rightId){
                str=str+array2[i];
                if (i!= array2.length-1){
                    str+=",";
                }
            }
        }
        role.setPsIds(str);
        int insert = 0;
        try {
            insert = roleMapper.updateById(role);
        }catch (Exception e){
            result.setINTERNALSERVERERROR();
        }
        if (insert==1){
            List<RightsVo> list = rightsMapper.selectPid();//查询所有的权限
            RoleVo roleVo = new RoleVo();
            roleVo.setRoleId(role.getRoleId());
            roleVo.setRoleDesc(role.getRoleDesc());
            roleVo.setRoleName(role.getRoleName());
            if (role.getPsIds()==null||role.getPsIds().equals("")){
                result.setSuccess("修改成功",roleVo);
                return result;
            }
            String[] arr1 = role.getPsIds().split(",");
            Integer[] arr= new Integer[arr1.length];
            for (int i = 0; i < arr1.length; i++) {
                arr[i]=Integer.parseInt(arr1[i]);
            }
            List<RightsVo> resultList = new ArrayList<>();
            for (RightsVo rightsVo:list){
                if (rightsVo.getLevel().equals("0")&&cha(arr,rightsVo.getId())){
                    List<RightsVo> secondList = new ArrayList<>();
                    for (RightsVo rightsVo1:list){
//                        System.out.println(rightsVo1.getId()+"|  |"+rightsVo1.getPid()+"|  |"+rightsVo.getId()+(rightsVo1.getPid()==rightsVo.getId()));
                        if (rightsVo1.getPid()==rightsVo.getId()&&cha(arr,rightsVo1.getId())){
                            List<RightsVo> thirdList = new ArrayList<>();
                            for (RightsVo rightsVo2:list){
                                if (rightsVo2.getPid()==rightsVo1.getId()&&cha(arr,rightsVo2.getId())){
                                    thirdList.add(rightsVo2);
                                }
                            }
                            rightsVo1.setChildren(thirdList);
                            secondList.add(rightsVo1);
                        }
                    }
                    rightsVo.setChildren(secondList);
                    resultList.add(rightsVo);
                }
            }
            roleVo.setChildren(resultList);
            result.setDELETED("删除权限成功",roleVo);
        }else {
            result.setINTERNALSERVERERROR();
        }
        return result;
    }


    public boolean cha(Integer[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]==null){
                continue;
            }
            if (arr[i]==id){
                return true;
            }
        }
        return false;
    }
}
